package com.example.hidesms.bo;

import java.util.ArrayList;

import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Note;

import com.example.hidesms.dao.SmsDAO;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.sqlite.DataBaseHelper;

public class SmsBO implements ISmsBO{
	private SMSItem				sms;
	private ArrayList<SMSItem>	listSms;
	private static SmsBO		instance;
	

	public static SmsBO getInstance() {
		if (instance == null) {
			instance = new SmsBO();
		}

		return instance;
	}
	private SMSItem fetchFromCurcor(Cursor c) {
		sms = new SMSItem();
		sms.setId(c.getInt(c.getColumnIndex("SmsID")));
		sms.setAddress(c.getString(c.getColumnIndex("Address")));
		sms.setAddressNameS(c.getString(c.getColumnIndex("AddressName")));
		sms.setMsg(c.getString(c.getColumnIndex("MsgBody")));
		sms.setReadState(c.getString(c.getColumnIndex("ReadState")));
		sms.setTime(c.getString(c.getColumnIndex("Time")));
		sms.setTimeFormated(c.getString(c.getColumnIndex("TimeFormated")));
		sms.setFolderName(c.getString(c.getColumnIndex("FolderName")));
		sms.setPhotoUri(c.getString(c.getColumnIndex("PhotoUri")));
		sms.setImage(c.getBlob(c.getColumnIndex("Image")));

		return sms;
	}

	@Override
	public boolean insert(DataBaseHelper myDatabase, SMSItem sms) {
		// TODO Auto-generated method stub
		return SmsDAO.getInstance().insert(myDatabase, sms);
	}

	@Override
	public boolean update(DataBaseHelper myDatabase, SMSItem sms) {
		// TODO Auto-generated method stub
		return SmsDAO.getInstance().update(myDatabase, sms);
	}

	@Override
	public boolean delete(DataBaseHelper myDatabase, int smsId) {
		// TODO Auto-generated method stub
		return SmsDAO.getInstance().delete(myDatabase, smsId);
	}

	@Override
	public ArrayList<SMSItem> getAll(DataBaseHelper myDatabase, String folderGroup, boolean flag) {
		// TODO Auto-generated method stub
		listSms = new ArrayList<SMSItem>();
		Cursor c= SmsDAO.getInstance().getAll(myDatabase, folderGroup, flag);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}

	@Override
	public ArrayList<SMSItem> getById(DataBaseHelper myDatabase, int smsId) {
		listSms = new ArrayList<SMSItem>();
		Cursor c= SmsDAO.getInstance().getById(myDatabase, smsId);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}
	@Override
	public ArrayList<SMSItem> getAllByRead(DataBaseHelper myDatabase, String read) {
		listSms = new ArrayList<SMSItem>();
		Cursor c= SmsDAO.getInstance().getAllByRead(myDatabase, read);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}
	@Override
	public ArrayList<SMSItem> getByAdress(DataBaseHelper myDatabase, String address) {
		listSms = new ArrayList<SMSItem>();
		Cursor c= SmsDAO.getInstance().getByAdress(myDatabase, address);
		if (c.moveToFirst()) {
			do{
				sms = fetchFromCurcor(c);
				listSms.add(sms);
			}while(c.moveToNext());
		}
		return listSms;
	}

}
